package med.movyzer.core;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import med.movyzer.datastructs.BestShiftCalculator;
import med.movyzer.datastructs.MovementSeries;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYAreaRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

@SuppressWarnings("serial")
public class SimilarityView extends JFrame{
	private ChartPanel chartPanel;
	JTextField tstart;
	JTextField tstop;
	JTextField tsize;
	JTextField tresult;
	private MovementSeries gt3xSeries;
	private MovementSeries psgSeries;
	private boolean isLeft=false;
	MovyzerMain main;
	
	
	public SimilarityView(MovyzerMain main,MovementSeries g,MovementSeries p,boolean isLeft){
		this.getContentPane().setLayout(null);
		this.initWindow();
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setVisible(true);
		
		this.main=main;
		this.gt3xSeries=g;
		this.psgSeries=p;
		this.isLeft=isLeft;
	}
	
	protected void initWindow() 
	{
		JLabel lstart=new JLabel("start time (ms): ");
		lstart.setBounds(5, 5, 100, 20);
		this.getContentPane().add(lstart);
		tstart=new JTextField("-10000");
		tstart.setBounds(120,5,140,20);
		this.getContentPane().add(tstart);
		
		JLabel lstop=new JLabel("end time (ms): ");
		lstop.setBounds(5, 30, 100, 20);
		this.getContentPane().add(lstop);
		tstop=new JTextField("10000");
		tstop.setBounds(120,30,140,20);
		this.getContentPane().add(tstop);
		
		JLabel lsize=new JLabel("step size (ms): ");
		lsize.setBounds(5, 50, 100, 20);
		this.getContentPane().add(lsize);
		tsize=new JTextField("1000");
		tsize.setBounds(120,50,140,20);
		this.getContentPane().add(tsize);
		
		JButton bcalc=new JButton("calc");
		bcalc.setBounds(270,5,110,70);
		bcalc.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					calcSimilarity();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		this.getContentPane().add(bcalc);
		
		chartPanel = new ChartPanel(null);
		chartPanel.setBounds(5, 85, 380, 300);
		((Component) chartPanel).setBackground(Color.decode("0x000000"));
		this.getContentPane().add(chartPanel);
		
		JLabel lresult=new JLabel("Best time shift: ");
		lresult.setBounds(5, 400, 100, 20);
		this.getContentPane().add(lresult);

		tresult=new JTextField("0");
		tresult.setBounds(120,400,140,20);
		this.getContentPane().add(tresult);
		
		JButton bset=new JButton("Set time shift");
		bset.setBounds(270,400,110,20);
		bset.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					setShift();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		this.getContentPane().add(bset);
		
		this.setBounds(0, 0, 400, 500);
	}
	
	public void calcSimilarity() throws Exception{
		XYSeries series= new XYSeries("Similarity");	
		
		int bestResult=BestShiftCalculator.getShiftSeries(new Integer(tstart.getText()),
				new Integer(tstop.getText()), new Integer(tsize.getText()), gt3xSeries, psgSeries,series);


		JFreeChart chart = ChartFactory.createXYAreaChart(
	            "Similarity measurement",       // chart title
	            "time shift (ms)",                    // domain axis label
	            "similarity (%)",                   // range axis label
	            new XYSeriesCollection(series),                   // data
	            PlotOrientation.VERTICAL,  // orientation
	            true,                      // include legend
	            true,                      // tooltips
	            false                      // urls
	        );
		
		XYAreaRenderer renderer = new XYAreaRenderer();
		renderer.setSeriesPaint(0, Color.RED.brighter());
		((XYAreaRenderer)renderer).setOutline(true);
		((XYAreaRenderer)renderer).setBaseOutlinePaint(Color.RED);
		
		((XYPlot)chart.getPlot()).setRenderer(renderer);
		
		chartPanel.setChart(chart);
		chartPanel.setRangeZoomable(false);	
		
		tresult.setText(Integer.toString(bestResult));
	}
	
	private void setShift() throws Exception{
		if(isLeft){
			main.setTimeShiftLeft(new Integer(tresult.getText()));
		}else{
			main.setTimeShiftRight(new Integer(tresult.getText()));
		}
	}

	public MovementSeries getGt3xSeries() {
		return gt3xSeries;
	}

	public void setGt3xSeries(MovementSeries gt3xSeries) {
		this.gt3xSeries = gt3xSeries;
	}
}
